{extend name="base:base" /}
{block name="body"}
<div class="layui-btn-group">
    <button class="layui-btn layui-btn-small layui-btn-normal" id="export">立即备份</button>
</div>
<form id="export-form" method="post" class="layui-form" action="{:Url::build('export')}">
    <table class="layui-table" lay-skin="line">
        <thead>
        <tr>
            <th ><input lay-skin="primary"  lay-filter="all_checkbox" checked type="checkbox"></th>
            <th style="text-align:center;">表名</th>
            <th>表备注</th>
            <th>储存引擎</th>
            <th>数据量</th>
            <th>数据大小</th>
            <th>创建时间</th>
            <th>备份状态</th>
        </tr>
        </thead>
        <tbody>
        {notempty name="data"}
        {volist name="data" id="vo"}
        <tr>
            <td><input checked type="checkbox" lay-skin="primary" name="ids[{$i-1}]" value="{$vo.name}"></td>
            <td align="center">{$vo.name}</td>
            <td>{$vo.comment}</td>
            <td>{$vo.engine ?? ''}</td>
            <td>{$vo.rows}</td>
            <td>{$vo.data_length|format_bytes}</td>
            <td>{$vo.create_time}</td>
            <td class="info">未备份</td>
        </tr>
        {/volist}
        {else /}
        <td colspan="6" class="text-center"> aOh! 暂时还没有内容! </td>
        {/notempty}
        </tbody>
    </table>
</form>
{/block}
{block name="script"}
<script type="text/javascript">
    UrlHighlight("{:Url::build('DataBase/index')}");
    layui.use(['jquery', 'form'], function () {
        var $form = layui.$("#export-form"),tables, $export = layui.$("#export");
        $export.click(function () {
            $export.parent().children().prop('disabled', true);
            $export.html("正在发送备份请求...");
            layui.$.post($form.attr("action"), $form.serialize(), function (data) {
                    if (data.code==1) {
                        tables = data.data.tables;
                        $export.html(data.msg + "开始备份，请不要关闭本页面！");
                        loading=layui.layer.load(2);
                        backup(data.data.tab);
                        window.onbeforeunload = function () {
                            return "正在备份数据库，请不要关闭！";
                        };
                    } else {
                        alert_msg(data.msg, 0);
                        $export.html("立即备份");
                        setTimeout(function () {
                            $export.parent().children().prop('disabled', false);
                        }, 1500);
                    }
                });
            return false;
        });

        function backup(tab, status) {
            status && showmsg(tab.id, "开始备份...(0%)");
            layui.$.get($form.attr("action"), tab, function (data) {
                if (data.code==1) {
                    showmsg(tab.id, data.msg);
                    if (!layui.$.isPlainObject(data.data.tab)) {
                        layui.layer.close(loading);
                        layui.layer.msg('恭喜您,备份完成!');
                        $export.parent().children().prop('disabled', false).html("备份完成，点击重新备份");
                        window.onbeforeunload = function () {
                            return null;
                        };
                    }
                    backup(data.data.tab, tab.id != data.data.tab.id);
                } else {
                    alert_msg(data.msg, 0);
                    $export.html("立即备份");
                    setTimeout(function () {
                        $export.parent().children().prop('disabled', false);
                    }, 1500);
                }
            });
        }

        function showmsg(id, msg) {
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })
</script>

{/block}